package com.chao.juc.asyn.yreturn;

import com.chao.juc.asyn.common.TaskHandler;

import java.util.concurrent.*;

/**
 * @author : Rookie
 * @description :
 * @date : 2023/2/4
 */
public class FutureTest {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<String> future = executorService.submit(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "测试future异步模式";
        });

        String result = null;
        try {
            long startTime = System.currentTimeMillis();
            result = future.get();
            System.out.println("获取异步结果成功，耗时" + (System.currentTimeMillis() - startTime) + "毫秒");
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(result);
        executorService.shutdown();
    }
}
